package api.cie.rss.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import android.util.Base64;

public class TwitterUtil {
	 
    private String userID;
    private String password;
 
    public TwitterUtil(String userID, String password)
    {
        this.userID = userID;
        this.password = password;
    }
 
    public void tweet(String message) throws Exception
    {
        URL url = new URL("https://twitter.com/statuses/update.xml");
        URLConnection connection = url.openConnection();
 
        connection.setDoInput(false);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
 
        String authorization = userID + ":" + password;
        String encoded = new String(Base64.encodeToString(authorization.getBytes(), Base64.DEFAULT).trim());
        connection.setRequestProperty("Authorization", "Basic " + encoded);
 
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write("status=" + URLEncoder.encode(message, "UTF-8"));
        out.close(); 
// 
//        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// 
//        //uncomment below to display debug output
// 
//        String response;
// 
//        while ((response = in.readLine()) != null)
//        {
//            System.out.println(response);
//        } 
// 
//        in.close();
    }
}